package com.campus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campus.common.config.CodeConfig;
import com.campus.entity.Account;
import com.campus.entity.ResultBody;
import com.campus.entity.Student;
import com.campus.entity.vo.AccountVO;
import com.campus.mapper.StudentMapper;
import com.campus.util.ElasticSearchUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @author Alaskaboo
 */
@RestController
@RequestMapping("/api")
public class ESController {

    @Autowired
    private ElasticSearchUtils electricSearchUtils;

    @Resource
    private StudentMapper studentMapper;
//
    @RequestMapping("/save")
    public ResultBody save(String  id,List<String> labels){
        Account build = Account.builder()
                .accountId(id)
                .label(labels)
                .build();
        electricSearchUtils.save(build);
        return ResultBody.ok();
    }

    @RequestMapping("/init")
    public ResultBody init(){
        String[] l = new String[]{"安静","晚睡","睡觉时间不固定","睡眠质量不好","外向","听歌","看书","运动","游戏","看电影","旅游","辩论"};
        Random random = new Random();
        for(int i=2020211891;i<=2020211899;i++){
            electricSearchUtils.delete(String.valueOf(i));
            int i1 = random.nextInt(l.length);
            int i2 = random.nextInt(l.length);
            while(i2==i1){
                i2=random.nextInt(l.length);
            }
            int i3 = random.nextInt(l.length);
            while(i3==i2||i3==i1){
                i3= random.nextInt(l.length);
            }
            electricSearchUtils.save(Account.builder().accountId(String.valueOf(i)).label(Arrays.asList(l[i1],l[i2],l[i3])).build());
        }
        return ResultBody.ok();
    }

    @RequestMapping("/delete/{id}")
    public ResultBody delete(@PathVariable String id){
        electricSearchUtils.delete(id);
        return ResultBody.ok().data("id",id).message(CodeConfig.DELETE_SUCCESS.message);
    }

    @RequestMapping("/match")
    public ResultBody match(@RequestBody List<String> labels){
        SearchHits<Account> matched = electricSearchUtils.match(Account.builder().label(labels).accountId("2020212037").build());
        if (matched.getTotalHits()==0){
            return ResultBody.ok();
        }
        float maxScore = matched.getMaxScore();
        float minScore = -1;
        ArrayList<AccountVO> data = new ArrayList<>();
        for (SearchHit<Account> a:matched){
            Account content = a.getContent();
            minScore=Math.min(minScore,a.getScore());
            Student account = studentMapper.selectOne(new QueryWrapper<Student>().eq("account", content.getAccountId()));
            data.add(new AccountVO(account.getId(),account.getAccount(),account.getName(),account.getAvatar(),a.getScore()+"",content.getLabel()));
        }
        for (AccountVO a :data){
            float v = (Float.parseFloat(a.getScore()) - minScore) / (maxScore - minScore);
            String format = String.format("%.2f", v*100);
            a.setScore(format);
        }
        return ResultBody.ok().data("match",data);
    }
}
